package com.wmwj.hxlj.service.impl;

import com.wmwj.hxlj.entity.Article;
import com.wmwj.hxlj.entity.History;
import com.wmwj.hxlj.entity.User;
import com.wmwj.hxlj.entity.vo.ArticleVo;
import com.wmwj.hxlj.entity.vo.PageResult;
import com.wmwj.hxlj.mapper.ArticleMapper;
import com.wmwj.hxlj.mapper.HistoryMapper;
import com.wmwj.hxlj.mapper.UserMapper;
import com.wmwj.hxlj.service.HistoryService;
import com.wmwj.hxlj.utils.BeanCopyUtils;
import com.wmwj.hxlj.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class HistoryServiceImpl implements HistoryService {

    @Autowired
    private HistoryMapper historyMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public Result<String> addHistory(History history) {
        try  {
            User user = userMapper.findById(history.getUserId());
            if (user == null){
                return Result.error("用户不存在");
            }
            if(articleMapper.findById(history.getContentId()) == null){
                return Result.error("文章不存在");
            }
            if (historyMapper.findPreById(history.getContentId()) != null){
                return Result.error("浏览记录已存在");
            }
            historyMapper.insert(history);
            return Result.success("添加成功");
        } catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("添加浏览记录失败");
        }
    }

    //获取用户浏览记录
    @Override
    public Result<PageResult<ArticleVo>> getHistoryByUserId(Long userId, Integer pageStart) {
        int pageStarts = (pageStart - 1)* 10;
        int pageSize = 10;
        try {
            List<History> historyList =  historyMapper.selectByUserId(userId, pageStarts, pageSize);
            if (historyList == null || historyList.isEmpty()){
                return Result.error("没有浏览记录");
            }

            List<Article> articleList = new ArrayList<>();

            //获取对应的文章信息列表
            for (History history : historyList) {
                Article article = articleMapper.findById(history.getContentId());
                articleList.add(article);
            }
            List<ArticleVo> articleVoList = BeanCopyUtils.copyBeanList(articleList, ArticleVo.class);

            int total = historyMapper.countByUserId(userId);
            PageResult<ArticleVo> pageResult = new PageResult<>(articleVoList, total);

            return Result.success(pageResult);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("获取用户浏览记录失败");
        }
    }

    @Override
    public Result<String>  deleteHistory(Long id) {
        try  {
            History history = historyMapper.findById(id);
            if (history == null){
                return Result.error("浏览记录不存在");
            }
            historyMapper.deleteById(id);
            return Result.success("删除成功");
        }catch (Exception e){
            throw new RuntimeException("删除浏览记录失败");
        }
    }
}
